package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.ActivityFeignClient;
import cn.com.dcsgo.ProductFeignClient;
import cn.com.dcsgo.SearchFeignClient;
import cn.com.dcsgo.service.SkuService;
import cn.com.dcsgo.vo.product.SkuInfoVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * SkuServiceImpl
 *
 * @author Dcsgo
 * @since 2025/5/7 上午8:40
 */
@Service
@RequiredArgsConstructor
public class SkuServiceImpl implements SkuService {
    private final ThreadPoolExecutor threadPoolExecutor;
    private final ProductFeignClient productFeignClient;
    private final ActivityFeignClient activityFeignClient;
    private final SearchFeignClient searchFeignClient;

    @Override
    public Map<String, Object> getSkuInfo(Long skuId, Long userId) {
        Map<String, Object> result = new HashMap<>();
        //获取商品信息
        CompletableFuture<Void> skuInfoFuture = CompletableFuture.runAsync(() -> {
            //远程调用获取商品信息
            SkuInfoVo skuInfoVo = productFeignClient.getSkuInfoVo(skuId);
            result.put("skuInfoVo", skuInfoVo);
        }, threadPoolExecutor);
        //获取营销信息->营销活动|优惠券
        CompletableFuture<Void> activityAndCouponInfoFuture = CompletableFuture.runAsync(() -> {
            //远程调用获取商品关联的优惠券信息
            Map<String, Object> activityAndCouponInfo = activityFeignClient.getActivityAndCouponInfo(skuId, userId);
            result.putAll(activityAndCouponInfo);
        }, threadPoolExecutor);
        //更新ES中商品的热度
        CompletableFuture<Void> skuPlusHotFuture = CompletableFuture.runAsync(() -> {
            //远程调用更新ES中关联商品的热度
            searchFeignClient.plusSkuHotScore(skuId);
        }, threadPoolExecutor);
        CompletableFuture.allOf(skuInfoFuture, activityAndCouponInfoFuture, skuPlusHotFuture).join();
        return result;
    }
}
